/*
 * This file (Alkwarel.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */
package org.algoristes.alkwarel;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.algoristes.alkwarel.gui.MainWindow;
import org.algoristes.alkwarel.utils.Log;

/**
 * Main class used to start the Alkwarel program.
 * 
 * @author Xavier Gouchet
 * 
 */
public class Alkwarel {

	/**
	 * Prints out the systems available look and feel. Among others, there is :
	 * <dl>
	 * <dt>Metal</dt>
	 * <dd>javax.swing.plaf.metal.MetalLookAndFeel <em>(Linux, Windows)</em></dd>
	 * <dt>CDE/Motif</dt>
	 * <dd>com.sun.java.swing.plaf.motif.MotifLookAndFeel
	 * <em>(Linux, Windows)</em></dd>
	 * <dt>GTK+</dt>
	 * <dd>com.sun.java.swing.plaf.gtk.GTKLookAndFeel
	 * <em>(<strong>Linux</strong>)</em></dd>
	 * <dt>Windows</dt>
	 * <dd>com.sun.java.swing.plaf.windows.WindowsLookAndFeel
	 * <em>(<strong>Windows</strong>)</em></dd>
	 * <dt>Windows Classic</dt>
	 * <dd>com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
	 * <em>(Windows)</em></dd>
	 */
	public static void PrintLookAndFeels() {
		System.out.println("Available Look And Feel on current system");
		UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
		for (int i = 0; i < info.length; i++) {
			UIManager.LookAndFeelInfo laf = info[i];
			System.out.println("\t" + laf.getName() + " -> "
					+ laf.getClassName());
		}
		System.out.println("Current system default Look And Feel");
		System.out.println("\t" + UIManager.getSystemLookAndFeelClassName());
	}

	/**
	 * Sets the look and feel to the selected one
	 * 
	 * @param laf
	 *            the look and feel to use (if null, using the systems default)
	 * 
	 */
	public static void SetLookAndFeel(String laf) {
		try {
			if ((laf == null) || (laf.isEmpty())) {
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());
			} else {
				UIManager.setLookAndFeel(laf);
			}
		} catch (ClassNotFoundException e) {
			Log.severe("Class not found when trying to set Look and Feel");
		} catch (ClassCastException e) {
			Log.warning("Provided class is not a Look and Feel class");
		} catch (InstantiationException e) {
			Log.warning("Provided class cannot be instanciated");
		} catch (IllegalAccessException e) {
			System.err.println(e.getMessage());
		} catch (UnsupportedLookAndFeelException e) {
			Log.warning("Provided Look and Feel is not supported");
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		boolean useGUI = true;

		Log.debug("Alkwarel > main > Analyzing the input args");
		for (String arg : args) {
			if (arg.toLowerCase().equals("nogui"))
				useGUI = false;
		}
		
		if (useGUI) {
			Log.info("Starting Alkwarel in graphic mode");
			JFrame.setDefaultLookAndFeelDecorated(true);
			SetLookAndFeel(null);
			MainWindow app = new MainWindow();
			app.addWindowListener(new WindowListener() {
				public void windowOpened(WindowEvent event) {
				}
				
				public void windowIconified(WindowEvent event) {
				}
				
				public void windowDeiconified(WindowEvent event) {
				}
				
				public void windowDeactivated(WindowEvent event) {
				}
				
				public void windowClosing(WindowEvent event) {
				}
				
				public void windowClosed(WindowEvent event) {
				}
				
				public void windowActivated(WindowEvent event) {
				}
			});
			app.setVisible(true);
		} else {
			Log.info("Starting Alkwarel in console mode");
		}
	}

}
